package com.elinkway.infinitemovies.view;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class CanDisabledViewPager extends ViewPager {

	private boolean isEnable=true;
	public CanDisabledViewPager(Context context) {
		super(context);
	}

	@Override
	public boolean onInterceptTouchEvent(MotionEvent arg0) {
		if (!isEnable){
			return false;
		}
		return super.onInterceptTouchEvent(arg0);
	}

	public CanDisabledViewPager(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	@Override
	public boolean onTouchEvent(MotionEvent arg0) {
		if (!isEnable){
			return false;
		}
		return super.onTouchEvent(arg0);
	}
	
	public void setEnableToScroll(boolean enable){
		this.isEnable=enable;
	}
	
	/*
	 * qinguoli
	 * 解决wrap_content失效导致 viewPager无法显示的问题
	 * */
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

		int height = 0;
		for (int i = 0; i < getChildCount(); i++) {
			View child = getChildAt(i);
			child.measure(widthMeasureSpec,
					MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
			int h = child.getMeasuredHeight();
			if (h > height)
				height = h;
		}

		heightMeasureSpec = MeasureSpec.makeMeasureSpec(height,
				MeasureSpec.EXACTLY);

		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	}
	
	
	
//	@Override
//	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//	    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//
//	    if (getCurrentItem() < getChildCount()) {
//	        View child = getChildAt(getCurrentItem());
//	        if (child.getVisibility() != GONE) {
//	            heightMeasureSpec = MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(heightMeasureSpec),
//	                    MeasureSpec.UNSPECIFIED);
//	            child.measure(widthMeasureSpec, heightMeasureSpec);
//	        }
//
//	        setMeasuredDimension(getMeasuredWidth(), measureHeight(heightMeasureSpec, getChildAt(getCurrentItem())));            
//	    }
//	}
	
//	 @Override
//	    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//	        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//
//	        // find the first child view
//	        View view = getChildAt(0);
//	        if (view != null) {
//	            // measure the first child view with the specified measure spec
//	            view.measure(widthMeasureSpec, heightMeasureSpec);
//	        }
//
//	        setMeasuredDimension(getMeasuredWidth(), measureHeight(heightMeasureSpec, view));
//	    }
	 
	 
	 /**
	     * Determines the height of this view
	     *
	     * @param measureSpec A measureSpec packed into an int
	     * @param view the base view with already measured height
	     *
	     * @return The height of the view, honoring constraints from measureSpec
	     */
//	    private int measureHeight(int measureSpec, View view) {
//	        int result = 0;
//	        int specMode = MeasureSpec.getMode(measureSpec);
//	        int specSize = MeasureSpec.getSize(measureSpec);
//
//	        if (specMode == MeasureSpec.EXACTLY) {
//	            result = specSize;
//	        } else {
//	            // set the height from the base view if available
//	            if (view != null) {
//	                result = view.getMeasuredHeight();
//	            }
//	            if (specMode == MeasureSpec.AT_MOST) {
//	                result = Math.min(result, specSize);
//	            }
//	        }
//	        return result;
//	    }
	 
	 
	
	
	
	/*private int measureHeight(int measureSpec, View view) {
		int result = 0;
		int specMode = MeasureSpec.getMode(measureSpec);
		int specSize = MeasureSpec.getSize(measureSpec);

		if (specMode == MeasureSpec.EXACTLY) {
			result = specSize;
		} else {
			// set the height from the base view if available
			if (view != null) {
				result = view.getMeasuredHeight();
			}
			if (specMode == MeasureSpec.AT_MOST) {
				result = Math.min(result, specSize);
			}
		}
		return result;
	}*/
	
	
	 /*@Override
     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
         super.onMeasure(widthMeasureSpec, heightMeasureSpec);

         View view = getChildAt(this.getCurrentItem());
         if (view != null) {
             view.measure(widthMeasureSpec, heightMeasureSpec);
         }
         setMeasuredDimension(getMeasuredWidth(), measureHeight(heightMeasureSpec, view));
     }

     private int measureHeight(int measureSpec, View view) {
         int result = 0;
         int specMode = MeasureSpec.getMode(measureSpec);
         int specSize = MeasureSpec.getSize(measureSpec);

         if (specMode == MeasureSpec.EXACTLY) {
             result = specSize;
         } else {
             // set the height from the base view if available
             if (view != null) {
                 result = view.getMeasuredHeight();
             }
             if (specMode == MeasureSpec.AT_MOST) {
                 result = Math.min(result, specSize);
             }
         }
         return result;
     }
	*/
	
	
	 
	 
}
